<template>
  <div class="app-container">
    <el-input
      v-model="filename"
      placeholder="Please enter the file name (default excel-list)"
      style="width: 350px;"
      :prefix-icon="Document" />
    <el-button
      :loading="downloadLoading"
      type="primary"
      :icon="Document"
      @click="handleDownload"
    >
      Export Selected Items
    </el-button>
    <a
      href="https://panjiachen.github.io/vue-element-admin-site/feature/component/excel.html"
      target="_blank"
      style="margin-left:15px;"
    >
      <el-tag type="info">Documentation</el-tag>
    </a>
    <el-table
      ref="multipleTable"
      v-loading="listLoading"
      :data="list"
      element-loading-text="拼命加载中"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" align="center" />
      <el-table-column align="center" label="Id" width="95">
        <template #default="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="Title">
        <template #default="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column label="Author" width="110" align="center">
        <template #default="scope">
          <el-tag>{{ scope.row.author }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="Readings" width="115" align="center">
        <template #default="scope">
          {{ scope.row.pageviews }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="PData" width="220">
        <template #default="scope">
          <el-icon style="vertical-align: middle;">
            <Timer />
          </el-icon>
          <span>{{ scope.row.display_time }}</span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { fetchList } from '@/api/article'
import { ElMessage } from 'element-plus'
import { Document, Timer } from '@element-plus/icons-vue'

const list = ref(null)
const listLoading = ref(true)
const multipleSelection = ref([])
const downloadLoading = ref(false)
const filename = ref('')
const listQuery = reactive({
  page: 1,
  limit: 20
})
const multipleTable = ref()

const fetchData = () => {
  listLoading.value = true
  fetchList(listQuery).then(response => {
    list.value = response.data.items
    listLoading.value = false
  })
}

const handleSelectionChange = val => {
  multipleSelection.value = val
}

const handleDownload = () => {
  if (multipleSelection.value.length) {
    downloadLoading.value = true
    import('@/vendor/Export2Excel').then(excel => {
      const tHeader = ['Id', 'Title', 'Author', 'Readings', 'Date']
      const filterVal = ['id', 'title', 'author', 'pageviews', 'display_time']
      const list = multipleSelection.value
      const data = formatJson(filterVal, list)
      excel.export_json_to_excel({
        header: tHeader,
        data,
        filename: filename.value
      })
      multipleTable.value.clearSelection()
      downloadLoading.value = false
    })
  } else {
    ElMessage({
      message: 'Please select at least one item',
      type: 'warning'
    })
  }
}

const formatJson = (filterVal, jsonData) => jsonData.map(v => filterVal.map(j => v[j]))

fetchData()
</script>

<script>
export default {
  name: 'SelectExcel'
}
</script>
